import com.im30.jenkins.pipeline.utils.BuildConfig

def call() {
    properties([
            gitLabConnection('GitLab'),
            buildDiscarder(logRotator(artifactDaysToim30Str: '', artifactNumToim30Str: '', daysToim30Str: '7', numToim30Str: '50'))
    ])

    dockerNode.nodeBuildDocker {
        BuildConfig buildConfig
        boolean isSuccess = false
        try {
            stage('Preparation') {
                // fetch remote tags
                checkout scm: [
                        $class           : 'GitSCM',
                        branches         : [[name: "origin/${env.gitlabSourceBranch}"]],
                        extensions       : [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
                        userRemoteConfigs: scm.userRemoteConfigs
                ]
            }

            buildConfig = BuildConfig.load(this)
            if (buildConfig == null) {
                throw new Exception("Cannot parse build.yaml")
            }

            List<Object> appInfos = buildConfig.getApps()

            gitlabBuilds(builds: ['Sonar', 'Lint','Build']) {

                stage('Sonar') {
                    gitlabCommitStatus("Sonar") {
                        echo 'Sonar Repo'
                        for (Object app : appInfos) {
                            echo "Try Sonar: ${app.name}"
                            if (app.sonar == 'Need') {
                                echo 'Sonar temp abandoned'
                            }
                        }
                    }
                }

                stage('Lint') {
                    gitlabCommitStatus("Lint") {
                        echo 'Lint Repo'
                        try {
                            for (Object app : appInfos) {
                                echo "Try Lint: ${app.name}"
                                if (app.lint == 'Need') {
                                    npmUtil.lint(jobUtil.getRepoName())
                                }
                            }
                        } catch (err) {
                            echo err.getMessage()
                        }
                    }
                }

                stage('Security Scan') {
                    parallel(
                            "murphyScan": {
                                stage('murphyScan') {
                                    try {
                                        securityUtil.murphyScan(jobUtil.getRepoName())
                                    } catch (err) {
                                        echo err.getMessage()
                                    }
                                }
                            }
                    )
                }

                stage('Build') {
                    gitlabCommitStatus("Build") {
                        if (buildConfig.isLibRepo()) {
                            if (jobUtil.getGitLabBranchName() == 'master') {
                                echo 'Deploy to Npm Repo'
                                if (!npmUtil.deployNpm(buildConfig)) {
                                    throw new Exception("Build Npm Failed!")
                                }
                            }
                        }

                        if (buildConfig.isAppRepo()) {
                            echo 'Build TAR and DockerImage'
                            jobUtil.buildApps(buildConfig)
                        }
                    }
                }
            }
        } finally {
            isSuccess = currentBuild.result == null || currentBuild.result == 'SUCCESS'
            if (buildConfig != null) {

                if (buildConfig.isLibRepo()) {
                    notifyUtil.notifyNpm(buildConfig.getNotifyConfig(), jobUtil.getRepoName(), jobUtil.getGitLabBranchName(), isSuccess)
                } else {
                    notifyUtil.notifyRepo(buildConfig.getNotifyConfig(), jobUtil.getRepoName(), jobUtil.getGitLabBranchName(), isSuccess)
                }
            }
            deleteDir()
        }
    }
}






